package top.wilbert.collegelink.positionresume.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;
import top.wilbert.collegelink.positionresume.entity.ResumeEntity;
import top.wilbert.collegelink.positionresume.feign.ThirdPartFeignService;
import top.wilbert.collegelink.positionresume.service.ResumeService;
import top.wilbert.collegelink.common.utils.PageUtils;
import top.wilbert.collegelink.common.utils.R;

import java.util.Arrays;
import java.util.Map;


/**
 * @author Augus
 * @email 2531496343@qq.com
 * @date 2022-12-23 20:02:17
 */
@RestController
@RequestMapping("positionresume/resume")
public class ResumeController {
    @Autowired
    private ResumeService resumeService;

    @Autowired
    private ThirdPartFeignService thirdPartFeignService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("subject:resume:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = resumeService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{resumeId}")
//    @RequiresPermissions("subject:resume:info")
    public R info(@PathVariable("resumeId") Long resumeId) {
        ResumeEntity resume = resumeService.getById(resumeId);

        return R.ok().put("resume", resume);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("subject:resume:save")
    public R save(@RequestBody ResumeEntity resume) {
        resumeService.save(resume);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("subject:resume:update")
    public R update(@RequestBody ResumeEntity resume) {
        resumeService.updateById(resume);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("subject:resume:delete")
    public R delete(@RequestBody Long[] resumeIds) {
        resumeService.removeByIds(Arrays.asList(resumeIds));

        return R.ok();
    }

    /**
     * 保存简历到阿里云，地址存储在数据库
     */
    @PostMapping("/uploadResume")
    public R uploadResume(@RequestPart("resume") MultipartFile resume,
                          @RequestParam("userId") Long userId) {
        if (resume == null) {
            return R.error("请上传简历");
        }

        //判断userId对应的简历数
        int count = resumeService.count(new QueryWrapper<ResumeEntity>().eq("user_id", userId));
        if (count < 3) {
            String resumeUrl = thirdPartFeignService.uploadResume(resume);
            resumeService.uploadResume(resumeUrl, userId);
            return R.ok().setData(resumeUrl);
        } else {
            return R.error("当前简历数超过3个，请删除已有简历后，再上传");
        }


    }

}
